<template>
  <div class="app-container">
    <el-form :inline="true" label-width="80px">
      <el-form-item label="银行名称">
        <el-input
          v-model="queryParams.bankname"
          placeholder="参数键名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态">
        <el-select @keyup.enter.native="handleQuery" v-model="queryParams.status" placeholder="状态" clearable
                   size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" size="mini" icon="el-icon-plus" @click="handleInsert"
                   v-hasPermi="['main:userBankConf:add']">添加
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="postList" @sort-change="sortChange">
      <el-table-column label="银行名称" align="center" prop="bankname"/>
      <el-table-column label="银行简称" align="center" prop="bankshort"/>
      <el-table-column label="银行编码" align="center" prop="bankcode"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status===1">可选择</span>
          <span v-else class="text-warning">不可选择</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createtime" sortable="custom"/>
      <el-table-column label="备注" align="center" prop="remark" :show-tooltip-when-overflow="true"/>
      <el-table-column label="操作" align="center" prop="idNo">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['main:userBankConf:edit']"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
            v-hasPermi="['main:userBankConf:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <el-dialog :title="title" :visible.sync="open" width="600px" :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="银行名称" prop="bankname">
          <el-input v-model="form.bankname" placeholder="请输入银行名称"/>
        </el-form-item>
        <el-form-item label="银行简称" prop="bankshort">
          <el-input v-model="form.bankshort" placeholder="请输入银行简称"/>
        </el-form-item>
        <el-form-item label="银行编码" prop="bankcode">
          <el-input v-model="form.bankcode" placeholder="请输入银行编码"/>
        </el-form-item>
        <el-form-item label="是否可选" prop="status">
          <el-radio-group v-model="form.status" size="small">
            <el-radio label="1" border>可选择</el-radio>
            <el-radio label="0" border>不可选择</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :disabled="addLoading" @click.native.prevent="submitForm">
          <span v-if="!addLoading">提 交</span>
          <span v-else>提 交 中...</span>
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listConfig, addConfig, putConfig, removeConfig} from "@/api/main/financial/bank";

  export default {
    data() {
      return {
        open: false,
        addLoading: false,
        loading: false,
        daterange: undefined,
        postList: [],
        statusOptions: [{"dictLabel": "可选择", "dictValue": "1"}, {"dictLabel": "不可选择", "dictValue": "0"}],
        title: '',
        total: 0,
        loadingInstance: undefined,
        queryParams: {
          bankname: undefined,
          status: undefined,
          pageNum: 0,
          pageSize: 10,
          orderByColumn: 'createtime DESC'
        },
        form: {
          bankname: '',
          bankshort: '',
          bankcode: '',
          status: "0"
        },
        rules: {
          bankname: [
            {required: true, message: '请输入银行名称', trigger: 'blur'},
            {max: 50, message: '长度最大为50个字符', trigger: 'blur'}
          ],
          bankshort: [
            {required: true, message: '请输入银行简称', trigger: 'blur'},
            {max: 20, message: '长度最大为20个字符', trigger: 'blur'}
          ],
          bankcode: [
            {required: true, message: '请输入银行编码', trigger: 'blur'},
            {max: 10, message: '长度最大为10个字符', trigger: 'blur'}
          ]
        }
      }
    },
    created() {
      this.getList();
    },
    methods: {
      getList() {
        this.loading = true;
        listConfig(this.queryDateRange(this.queryParams, this.daterange)).then(response => {
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      handleDelete(row) {
        let _this = this;
        this.$confirm(`确定要删除银行名称为[${row.bankname}]的记录?`, "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          removeConfig(row.id).then(response => {
            _this.msgSuccess("删除成功");
            _this.getList();
          });
        }).catch(function (e) {
          console.log(e);
        });
      },
      handleInsert() {
        this.reset();
        this.title = '新增';
        this.open = true;
      },
      handleUpdate(row) {
        this.title = '编辑';
        this.form = {
          id: row.id,
          bankname: row.bankname,
          bankshort: row.bankshort,
          bankcode: row.bankcode,
          status: row.status+""
        };
        this.open = true;

      },
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      cancel() {
        this.open = false;
        this.reset();
      },
      reset() {
        this.form = {
          bankname: '',
          bankshort: '',
          bankcode: '',
          status: "0"
        };
        this.fileList = [];
        this.resetForm("form");
      },
      submitForm() {
        let _this = this;
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id) {
              putConfig(this.form).then(response => {
                this.msgSuccess("编辑成功");
                this.addLoading = false;
                this.open = false;
                this.getList();
              }).catch(function (e) {
                console.log(e);
                _this.addLoading = false;
              });
            } else {
              this.addLoading = true;
              addConfig(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.addLoading = false;
                this.open = false;
                this.getList();
              }).catch(function (e) {
                console.log(e);
                _this.addLoading = false;
              });
            }
          }
        });
      },
      sortChange(params) {
        if (params.prop === 'createtime') {
          let createtime = 'createtime';
          if (params.order === 'ascending') {
            this.queryParams.orderByColumn = createtime + ' ASC';
          } else {
            this.queryParams.orderByColumn = createtime + ' DESC';
          }
        }
        this.getList();
      }
    }
  }
</script>

<style>

</style>
